package com.xiaoxu.intranetweb.mybatisplusSzappdb.dao;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.*;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.*;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.UserInfoVo;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户信息dao层
 * @author xiaoxu
 * @date 2023/1/13 15:00
 * @version 1.0
 * @since 1.0
 */

@Slf4j
@Component
public class UserInfoDao {
    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private ISysUserRoleConnService iSysUserRoleConnService;
    @Autowired
    private ISysRoleService iSysRoleService;
    @Autowired
    private ISysUserOrgConnService iSysUserOrgConnService;
    @Autowired
    private ICCompanysService iCCompanysService;
    @Autowired
    private ISysUserTruckConnService iSysUserTruckConnService;
    @Autowired
    private ICTrucksService iCTrucksService;
    @Autowired
    private ISysRoleInfoFunctionService iSysRoleInfoFunctionService;
    @Autowired
    private ITPlantsService iTPlantsService;
    @Autowired
    private ISysUserPlantConnService iSysUserPlantConnService;
    @Autowired
    private ISysRoleInfoFunctionWebService iSysRoleInfoFunctionWebService;
//    拿到user信息
    public SysUser getUser(String userId) throws Exception {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
//        除了password和pwdDetail外的所有信息
//        queryWrapper.select(SysUser.class,info->!info.getColumn().equals("PASSWORD")&&!info.getColumn().equals("PWD_DETAIL"));
        queryWrapper.eq("ID", userId);
        SysUser sysUser = iSysUserService.getOne(queryWrapper);
        if(sysUser == null){
            throw new Exception("用户不存在");
        }
        return sysUser;
    }
//    拿到user的角色信息
    public SysRole getUserRole(String userId) throws Exception {
        QueryWrapper<SysUserRoleConn> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("USER_ID", userId);
        SysUserRoleConn sysUserRoleConn = iSysUserRoleConnService.getOne(queryWrapper);
        if(sysUserRoleConn == null){
            throw new Exception("用户角未绑定角色");
        }
        QueryWrapper<SysRole> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("ID", sysUserRoleConn.getRoleId());
        SysRole sysRole = iSysRoleService.getOne(queryWrapper1);
        return sysRole;
    }
//    拿到user绑定的企业信息
    public List<CCompanys> getUserCompany(String userId) throws Exception {
        QueryWrapper<SysUserOrgConn> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("USER_ID", userId);
        SysUserOrgConn  sysUserOrgConn = iSysUserOrgConnService.getOne(queryWrapper);
        if(sysUserOrgConn == null){
            throw new Exception("用户未绑定企业");
        }
        QueryWrapper<CCompanys> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("ID", sysUserOrgConn.getOrgId());
        List<CCompanys> cCompanys = iCCompanysService.list(queryWrapper1);
        return cCompanys;
    }
//    拿到用户与车辆的绑定信息
    public List<CTrucks> getUserCar(String userId)  {
        try {
            QueryWrapper<SysUserTruckConn> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("USER_ID", userId);
            SysUserTruckConn sysUserTruckConn = iSysUserTruckConnService.getOne(queryWrapper1);
            QueryWrapper<CTrucks> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("ID", sysUserTruckConn.getTruckId());
            List<CTrucks> cTrucks = iCTrucksService.list(queryWrapper);
            return cTrucks;
        }
        catch (Exception e){
            return null;
        }
    }
//    拿到用户与厂的绑定信息
    public TPlants getUserFactory(String userId) throws Exception {
        QueryWrapper<SysUserPlantConn> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("USER_ID", userId);
        SysUserPlantConn sysUserPlantConn = iSysUserPlantConnService.getOne(queryWrapper);
        if(sysUserPlantConn == null){
            throw new Exception("用户未绑定厂");
        }
        QueryWrapper<TPlants> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("ID", sysUserPlantConn.getPlantId());
        queryWrapper1.eq("DEL_FLAG", 0);
        TPlants tPlants = iTPlantsService.getOne(queryWrapper1);
        return tPlants;
    }
//    拿到用户的权限信息
    public Map getUserPermission(String userId) throws Exception {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", userId);
        SysUser sysUser = iSysUserService.getOne(queryWrapper);
        if(sysUser == null){
            throw new Exception("用户不存在");
        }
        String role=sysUser.getAppAuthority1();
//        权限码转换成数组
        String[] routes = String.valueOf(role).split("");
//        便利routes数组，拿到权限码，放到list中
        Map<String, String> map=new HashMap<>();
//        新建一个list
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < routes.length; i++) {
            if (routes[i].equals("1")) {
                list.add(i);
            }
        }
//        根据list中的权限码，查询权限表，拿到权限名称
        for (int i = 0; i < list.size(); i++) {
            QueryWrapper<SysRoleInfoFunction> queryWrapper1=new QueryWrapper<>();
            queryWrapper1.eq("WEB_FUNCTION_NUMBER",list.get(i).toString());
            queryWrapper1.and(wrapper->wrapper.isNull("WEB_FUNCTION_DEL_FLAG").or().eq("WEB_FUNCTION_DEL_FLAG","0"));
            SysRoleInfoFunction sysRoleInfoFunction = iSysRoleInfoFunctionService.getOne(queryWrapper1);
            if (sysRoleInfoFunction!=null){
                map.put(sysRoleInfoFunction.getWebFunctionName(),sysRoleInfoFunction.getWebFunctionName());
            }
        }
        QueryWrapper<SysRoleInfoFunction> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.in("WEB_FUNCTION_NUMBER", list);
        queryWrapper1.and(wrapper -> wrapper.isNull("WEB_FUNCTION_DEL_FLAG").or().eq("WEB_FUNCTION_DEL_FLAG", "0"));
        List<SysRoleInfoFunction> resultList = iSysRoleInfoFunctionService.list(queryWrapper1);
        Map<String, Object> resultMap = new HashMap<>();
        resultList.forEach(sysRoleInfoFunction -> resultMap.put(sysRoleInfoFunction.getWebFunctionName(), null));
        return map;
    }
    public Map getUserPermissionWeb(String userId) throws Exception {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", userId);
        SysUser sysUser = iSysUserService.getOne(queryWrapper);
        if(sysUser == null){
            throw new Exception("用户不存在");
        }
        String role=sysUser.getAppAuthority2();
//        权限码转换成数组
        String[] routes = String.valueOf(role).split("");
//        便利routes数组，拿到权限码，放到list中
        Map<String, String> map=new HashMap<>();
//        新建一个list
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < routes.length; i++) {
            if (routes[i].equals("1")) {
                list.add(i);
            }
        }
//        根据list中的权限码，查询权限表，拿到权限名称
            QueryWrapper<SysRoleInfoFunctionWeb> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.in("WEB_FUNCTION_NUMBER",list);
//            WEB_FUNCTION_DEL_FLAG为空或者为0
            queryWrapper1.and(wrapper->wrapper.isNull("WEB_FUNCTION_DEL_FLAG").or().eq("WEB_FUNCTION_DEL_FLAG","0").or().eq("WEB_FUNCTION_DEL_FLAG",""));
            List<SysRoleInfoFunctionWeb> sysRoleInfoFunctionweb = iSysRoleInfoFunctionWebService.list(queryWrapper1);
        for (SysRoleInfoFunctionWeb sysRoleInfoFunctionWeb : sysRoleInfoFunctionweb) {
                map.put(sysRoleInfoFunctionWeb.getWebFunctionName(),sysRoleInfoFunctionWeb.getWebFunctionEnableFlag());
            }
        return map;
    }
//    封装上面的方法，拿到user的所有信息
    public UserInfoVo getUserInfoByToken(String token) throws Exception {
//        token解析出userId

        String userId = StpUtil.getLoginIdByToken(token).toString();
            UserInfoVo userInfoVo = new UserInfoVo();
        SysUser sysUser = getUser(userId);
            userInfoVo.setSysUser(sysUser);
        SysRole sysRole = getUserRole(userId);
            userInfoVo.setSysRole(sysRole);
        List<CCompanys> cCompanys = getUserCompany(userId);
            userInfoVo.setCCompanys(cCompanys);
        if (getUserCar(userId) != null) {
            List<CTrucks> cTrucks = getUserCar(userId);
            userInfoVo.setCTrucks(cTrucks);
        }
//        TPlants tPlants = getUserFactory(userId);
//            userInfoVo.setTPlants(tPlants);
        Map userPermission = getUserPermission(userId);
            userInfoVo.setUserPermission(userPermission);
            return userInfoVo;
    }
}
